#include "mat_sfx_manager.h"
#include "mat_sfx_collection.h"
#include "mat_define.h"
#include "gen_game_state.h"

#include "vfx/text_sfx.h"

#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "graphics/graphics_util.h"



MATSFXManager&	MATSFXManager::Instance()
{
	static MATSFXManager	Inst;
	return Inst;
}

MATSFXManager::MATSFXManager()
{
	// weired bug if i dun write something here
	int i = 0;
	i = 2;	
	
}

MATSFXManager::~MATSFXManager()
{
	Clear();
	for(unsigned int i = 0; i < m_CacheSfxs.size(); i++)
	{
		m_CacheSfxs[i]->Clear();
		UTIL_SAFE_DELETE(m_CacheSfxs[i]);
	}
	m_CacheSfxs.resize(0);
	
}

void		MATSFXManager::Render(const math::Vec2& Offset,float Scale)
{
	MATSFXCollection* pCol;
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		pCol = m_SFXCollections[i];
		pCol->Render(Offset,Scale);
	}

	m_NumberSFX.Render(Offset,1.0f);
}

void		MATSFXManager::PostRender(const math::Vec2& Offset,float Scale)
{
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		MATSFXCollection* pCol = m_SFXCollections[i];
		pCol->PostRender(Offset,Scale);
	}

}
	
void		MATSFXManager::RenderDebug(const math::Vec2& Offset,float Scale)
{
	MATSFXCollection* pCol;
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		pCol = m_SFXCollections[i];
		pCol->RenderDebug(Offset,Scale);
	}
}


void		MATSFXManager::Tick(float dt)
{
	MATSFXCollection* pCol;
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		pCol = m_SFXCollections[i];
		pCol->Tick(dt);
	}

	m_NumberSFX.Tick(dt);
}


void		MATSFXManager::Clear()
{
	m_CacheSfxs.resize(0);
	for(unsigned int i = 0; i < m_SFXCollections.size(); i++)
	{
		MATSFXCollection* pCol = m_SFXCollections[i];
		pCol->Clear();
	}

	for( int i = (int)m_SFXCollections.size() - 1; i >= 0; i--)
	{
		MATSFXCollection* pCol = m_SFXCollections[i];
		UTIL_SAFE_DELETE(pCol);
	}
	m_SFXCollections.resize(0);
}

void		MATSFXManager::ForceFinish()
{
	for( int i = (int)m_SFXCollections.size() - 1; i >= 0; i--)
	{
		MATSFXCollection* pCol = m_SFXCollections[i];
		pCol->ForceFinish();
	}	
}

MATSfx*		MATSFXManager::Spawn(MATSfx::SFX_TYPES Type, 
											const math::Vec2& Pos,
											float SizeScale,
											float Rotation,
											bool	bIsCache,
											MATSounds::SOUND_NAMES SoundSFXName
											)
{
	MATSfx* pSFX = NULL;
	bool Found = false;
	for( int i = (int)m_SFXCollections.size() - 1; i >= 0; i--)
	{
		MATSFXCollection* pCol = m_SFXCollections[i];
		if ( pCol->GetType() == Type)
		{
			pSFX = pCol->Spawn(	Pos, 
								SizeScale, 
								Rotation,
								SoundSFXName
								);
			Found = true;
			i = -1;
		}		
	}

	if(!Found)
	{
		MATSFXCollection* pCol = new MATSFXCollection(Type);		
		pSFX = pCol->Spawn(	Pos, 
							SizeScale, 
							Rotation,
							SoundSFXName
							);

		if(!bIsCache)
			m_SFXCollections.push_back(pCol);
	}

	return pSFX;		
}

MATSfx*		MATSFXManager::GetCacheSfx(MATSfx::SFX_TYPES Type)
{
	if((int)m_CacheSfxs.size() < Type + 1)
		return NULL;

	return m_CacheSfxs[(int)Type];
}

void		MATSFXManager::Cache()

{
	m_CacheSfxs.resize(0);
	for(unsigned int i = 0; i < MATSfx::SFX_COUNT; i++)
	{
		MATSfx* pSfx = Spawn((MATSfx::SFX_TYPES)i, math::Vec2(0.5f,0.5f), 1.0f, 0, true);	
		m_CacheSfxs.push_back(pSfx); 	
		ui::UIManager::Instance().UpdateLoadCB();
		pSfx->ToDie();
	}
}

void		MATSFXManager::AddFlySfxNumber(int Number, const math::Vec2& Pos)
{
	m_NumberSFX.AddNumber(Number, Pos );
}

MATNumberSFX::MATNumberSFX() :  m_pSfxFlyNumber(NULL)
{
	m_pSfxFlyNumber = new vfx::NumberFXSet(this); 
	m_pSfxFlyNumber->Load("ui_img/time_number.lua", 16);	
	m_Set.push_back(m_pSfxFlyNumber);
}
	
MATNumberSFX::~MATNumberSFX()
{
	for (unsigned int i=0; i<m_Set.size(); i++)
	{
		UTIL_SAFE_DELETE(m_Set[i]);
	}
	m_Set.resize(0);
}
	
void	MATNumberSFX::AddNumber(int Number, const math::Vec2& Pos)
{
	m_pSfxFlyNumber->AddNumber(Number, Pos, Pos - math::Vec2(0, 30 * GraphicsUtil::InvH),
								ColorfRGBA(1.0f,1.0f,1.0f,1.0f),1.0f,true);	
}